<template>
    <section class="chart-container">
        <el-row>
            
            <el-col :span="24">
                <div id="chartPie" style="width:100%; height:280px;"></div>
            </el-col>
            
        </el-row>
    </section>
</template>

<script>
    import echarts from 'echarts'
    import {getChars} from '../../../api/api'
    export default {
        data() {
            return {
                chartPie: null
            }
        },

        methods: {
            getCharData() {//获取列表数据
                //var _this = this;
                var charLegend=[];//图例
                var charData=[];//数据
                getChars().then((res) => {
                    console.log("图表数据-饼状图")
                    res.data.result.forEach(function(e){  
                        charLegend.push(e.channel);
                        charData.push({value:e.count,name:e.channel})
                    })
                    this.drawPieChart(charLegend,charData)
                });
            },
            drawPieChart(charLegend,charData) {
                this.chartPie = echarts.init(document.getElementById('chartPie'));
                this.chartPie.setOption({
                    title: {
                        text: 'Pie Chart',
                        subtext: '渠道',
                        x: 'center'
                    },
                    tooltip: {
                        trigger: 'item',
                        formatter: "{a} <br/>{b} : {c} ({d}%)"
                    },
                    legend: {
                        orient: 'vertical',
                        left: 'left',
                        data:charLegend
                    },
                    series: [
                        {
                            name: '访问来源',
                            type: 'pie',
                            radius: '55%',
                            center: ['50%', '60%'],
                            data: charData,
                            itemStyle: {
                                emphasis: {
                                    shadowBlur: 10,
                                    shadowOffsetX: 0,
                                    shadowColor: 'rgba(0, 0, 0, 0.5)'
                                }
                            }
                        }
                    ]
                });
            }
        },

        mounted: function () {
            this.getCharData()
        }
    }
</script>

<style scoped>
    .chart-container {
        width: 100%;
        float: left;
    }
    /*.chart div {
        height: 400px;
        float: left;
    }*/

    .el-col {
        padding: 30px 20px;
    }
</style>
